package com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.kongzue.dialog.v3.WaitDialog;
import com.ninetaildemonfox.zdl.socialproject.base.BaseActivity;
import com.ninetaildemonfox.zdl.socialproject.R;
import com.ninetaildemonfox.zdl.socialproject.mvp.contract.AddAgentContract;
import com.ninetaildemonfox.zdl.socialproject.mvp.presenter.AddAgentPresenter;
import com.ninetaildemonfox.zdl.socialproject.utils.ToastUtil;

import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author NineTailDemonFox
 * @date 2019/7/24 11:51
 * 功能描述： 申请代理
 * 联系方式：1037438704@qq.com
 */
public class RegionalAgentActivity extends BaseActivity implements AddAgentContract {
    @BindView(R.id.image_left_finish)
    ImageView imageleftFinish;
    @BindView(R.id.text_center)
    TextView textCenter;
    @BindView(R.id.text_right)
    TextView textRight;
    @BindView(R.id.edit_contact)
    EditText editContact;
    @BindView(R.id.edit_name)
    EditText editName;
    @BindView(R.id.edit_information)
    EditText editInformation;
    @BindView(R.id.text_application_submission)
    TextView textApplicationSubmission;
    private AddAgentPresenter presenter;
    private int count = 0;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_regional_agent;
    }

    @Override
    protected void onInitialization(Bundle bundle) {
        textCenter.setText("申请代理");
        imageleftFinish.setVisibility(View.VISIBLE);
        presenter = new AddAgentPresenter(me, this, this);
        WaitDialog.show(me, "数据请求中...");
        presenter.addAgent(token,
                "", "", "", "1");
    }

    @OnClick({R.id.image_left_finish, R.id.text_application_submission})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.image_left_finish:
                finish();
                break;
            case R.id.text_application_submission:
                if (count == 1) {
                    return;
                }
                if (TextUtils.isEmpty(editContact.getText().toString().trim())) {
                    ToastUtil.show("联系方式不能为空");
                    return;
                }
                if (TextUtils.isEmpty(editName.getText().toString().trim())) {
                    ToastUtil.show("名字不能为空");
                    return;
                }
                if (TextUtils.isEmpty(editInformation.getText().toString().trim())) {
                    ToastUtil.show("申请区域不能为空");
                    return;
                }

                if (getSex.equals("1")) {
                    if (getVip.equals("1")) {
                        ToastUtil.show("请先成为会员");
                        return;
                    }
                } else {
                    if (getIdentification.equals("1")) {
                        ToastUtil.show("请先认证");
                        return;
                    }
                }
                presenter.addAgent(token,
                        editContact.getText().toString().trim(),
                        editName.getText().toString().trim(),
                        editInformation.getText().toString().trim(),
                        "2");
                break;
            default:
        }
    }

    @Override
    public void AddAgentSuccess(Map<String, String> data) {
        editContact.setText(data.get("tel"));
        editName.setText(data.get("name"));
        editInformation.setText(data.get("information"));
        if (data.get("tel") != null) {
            editContact.setFocusable(false);
            editContact.setFocusableInTouchMode(false);
            editName.setFocusable(false);
            editName.setFocusableInTouchMode(false);
            editInformation.setFocusable(false);
            editInformation.setFocusableInTouchMode(false);
            count = 1;
            textApplicationSubmission.setText("已提交申请");
            textApplicationSubmission.setBackgroundResource(R.color.dialogButtonGrayLightPress);
        }

    }

    @Override
    public void AddAgentFailed(String msg) {

    }

    @Override
    public void showLoading() {

    }
}
